﻿var centerX = 0;
var centerY = 0;
var viewHalfWidth = 50;
var viewHalfHeight = 50;

var canvWidth = 0;
var canvHeight = 0;

function draw()
{
	var cs = document.getElementById("canv");
	var ctx = cs.getContext("2d");
	
	width = cs.width;
	height = cs.height;
	
	canvWidth = cs.width;
	canvHeight = cs.height;
	
	ctx.clearRect(0,0,width,height);
	
	ctx.fillStyle = "#CCEEEE";
	ctx.fillRect(0,0,width,height);
	
	//Draw the X axis
	
	ctx.beginPath();
	ctx.strokeStyle = "#FF0000";
	ctx.moveTo(0, - (centerY - viewHalfHeight)/ ((centerY + viewHalfHeight) - (centerY - viewHalfHeight)) * height);
	ctx.lineTo(width, - (centerY - viewHalfHeight)/ ((centerY + viewHalfHeight) - (centerY - viewHalfHeight)) * height);
	ctx.stroke();
	ctx.closePath();
	
	//Draw the Y axis
	ctx.beginPath();
	ctx.strokeStyle = "#00FF00";
	ctx.moveTo(- (centerX - viewHalfWidth)/ ((centerX + viewHalfWidth) - (centerX - viewHalfWidth)) * width, 0);
	ctx.lineTo(- (centerX - viewHalfWidth)/ ((centerX + viewHalfWidth) - (centerX - viewHalfWidth)) * width, height);
	ctx.stroke();	
	ctx.closePath();	
	
	
	//Draw the agents
	for(var i = 0; i < actors.length; i++)
	{
		var actor = actors[i];
		var params = actors[i].ActorParams;
		
		ctx.beginPath();
		ctx.fillStyle="#000000";
		ctx.strokeStyle="#000000";
		ctx.arc(
			(params.x  - (centerX - viewHalfWidth))/ ((centerX + viewHalfWidth) - (centerX - viewHalfWidth)) * width,
			(params.y  - (centerY - viewHalfHeight))/ ((centerY + viewHalfHeight) - (centerY - viewHalfHeight)) * height,
			params.radius/(viewHalfWidth * 2) * canvWidth,0,2*Math.PI, false);
		ctx.stroke();
		ctx.closePath();
		
		//Now draw the heading
		ctx.beginPath();
		ctx.fillStyle="#000000";
		ctx.strokeStyle = "#000000";
		var moveX = ToScreenX(params.x);
		var moveY = ToScreenY(params.y);
		ctx.moveTo(moveX, moveY);
		var lineX = ToScreenX(params.x + Math.cos(params.velocityAngle) *params.velocityRadius * params.radius*2);
		var lineY = ToScreenY(params.y + Math.sin(params.velocityAngle) *params.velocityRadius * params.radius * 2);
		ctx.lineTo(
			lineX,
			lineY);
		ctx.stroke();
		ctx.closePath();
	}	
	
	
}

function ToScreenX(x)
{
	return (x  - (centerX - viewHalfWidth))/ ((centerX + viewHalfWidth) - (centerX - viewHalfWidth)) * width;
}

function ToScreenY(y)
{
	return (y  - (centerY - viewHalfHeight))/ ((centerY + viewHalfHeight) - (centerY - viewHalfHeight)) * height;
}
